package com.liull.papersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liull.papersystem.entity.UserDo;
import com.liull.papersystem.mapper.UserMapper;
import com.liull.papersystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Descriotion:
 * @Author longlong.liu
 * @Version 1.0
 * @Date 2021/10/26 21:06
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    private final static BCryptPasswordEncoder ENCODER = new BCryptPasswordEncoder();

    @Override
    public UserDo login(String username, String pwd) {
        QueryWrapper<UserDo> wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        UserDo userDo = userMapper.selectOne(wrapper);
        if (userDo != null && ENCODER.matches(pwd, userDo.getPassword())) {
            return userDo;
        }
        return null;
    }
}
